package com.raowb.common.base.services.impl;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.core.util.ReflectUtil;
import com.raowb.common.base.services.BaseIDService;

/**
 * 
 * @Description:
 * @author: raowb
 * @date 2018年8月6日
 */
public class BaseIDServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseIDService<T> {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	// @Transactional(rollbackFor = Exception.class)
	public boolean saveBatch(Collection<T> entityList) {
		return saveBatch(entityList, 1000);
	}

	@Override
	public boolean delete(Serializable... ids) {
		// TODO Auto-generated method stub
		return removeByIds(Arrays.asList(ids));
	}

	@Override
	public boolean delete(Wrapper<T> wrapper) {
		return remove(wrapper);
	}

	@Override
	public boolean update(T entity) {
		// TODO Auto-generated method stub
		return updateById(entity);
	}

	@Override
	public boolean update(T entity, Map<String, Object> fields) {
		UpdateWrapper<T> uw = new UpdateWrapper<>();
		uw.eq("id", ReflectUtil.getFieldValue(entity, "id"));
		if (fields != null) {
			for (String key : fields.keySet()) {
				uw.set(key, fields.get(key));
			}
		}
		return update(entity, uw);
	}

	@Override
	public boolean updateBatch(Collection<T> entityList) {
		// TODO Auto-generated method stub
		return updateBatch(entityList, 1000);
	}

	@Override
	public boolean updateBatch(Collection<T> entityList, int batchSize) {
		// TODO Auto-generated method stub
		return updateBatchById(entityList, batchSize);
	}

	@Override
	public boolean saveOrUpdateBatch(Collection<T> entityList) {
		return saveOrUpdateBatch(entityList, 1000);
	}

	@Override
	public T findById(Serializable id) {
		// TODO Auto-generated method stub
		return getById(id);
	}

	@Override
	public T findOne(Wrapper<T> queryWrapper) {
		// TODO Auto-generated method stub
		return getOne(queryWrapper);
	}

	@Override
	public int findCount(Wrapper<T> queryWrapper) {
		// TODO Auto-generated method stub
		return Long.valueOf(count(queryWrapper)).intValue();
	}

	@Override
	public List<T> findByIds(Serializable... ids) {
		// TODO Auto-generated method stub
		return (List<T>) listByIds(Arrays.asList(ids));
	}

	@Override
	public List<T> findList(Wrapper<T> queryWrapper) {
		// TODO Auto-generated method stub
		return list(queryWrapper);
	}

	@Override
	public IPage<T> findPage(IPage<T> page, Wrapper<T> queryWrapper) {
		// TODO Auto-generated method stub
		page.setTotal(0L);// 解决删除、查询等 不重置total问题
		return page(page, queryWrapper);
	}

	public M getDao() {
		return baseMapper;
	}

	@Override
	public boolean save(T entity) {
		// TODO Auto-generated method stub
		return super.save(entity);
	}

}
